<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\AdminMenu as AdminMenuModel;

class Menu extends AdminBase{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '菜单管理', 'url' => url('admin/menu/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '添加顶级菜单', 'url' => url('admin/menu/add','pid=0'), 'icon' => '&#xe654;'];
        $this->submenu[] = ['title' => '更新菜单缓存', 'url' => url('admin/menu/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 菜单首页
     * @access public
     * @return \think\Response
     */
	public function index(){
        $admin_menu_model = new AdminMenuModel;
        if($this->request->isAjax()){
            $action = $this->request->param('action');
            if($action == 'del'){
                $ids = $this->request->param('ids/a');
                if(empty($ids)){
                    return mn_ajax_msg(0,'你还没有选择呢');
                }
                $admin_menu_model->del($ids);
                $admin_menu_model->cache();
                return mn_ajax_msg(1,'删除成功');
            }elseif($action == 'order'){
                $order = $this->request->param('data/a');
                if(empty($order)){
                    return mn_ajax_msg(0,'你还没有选择呢');
                }
                foreach ($order as $key => $value) {
                    if(!empty($value['displayorder'])){
                        $admin_menu_model->allowField(true)->save(['displayorder'=>(int)$value['displayorder']],['id'=>(int)$key]);
                    }
                }
                $admin_menu_model->cache();
                return mn_ajax_msg(1,'更新排序成功');
            }else{
                return mn_ajax_msg(0,'未知操作');
            }
        }
        
        $list = cache('admin_menu');
        $this->assign([
            'list' => $list,
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
	}

    /**
     * 添加
     * @access public
     * @return \think\Response
     */
    public function add($pid = 0){
        $pid = (int)$pid;

        $admin_menu_model = new AdminMenuModel;
        //获取菜单类型
        $top = $admin_menu_model->getTopId();
        $menu_name = $menu_type = '';
        $data['pid'] = $pid;
        if ($data['pid']) {
            if (in_array($data['pid'], $top)) {
                $menu_type = 0;
                $menu_name = '分组菜单';
            } else {
                $menu_type = 1;
                $menu_name ='链接菜单';
            }
        } else {
            $menu_type = 0;
            $menu_name = '顶级菜单';
        }

        //判断表单提交
        if($this->request->isPost()){

            $post_data = $this->request->param('data/a');
            //验证菜单
            if($menu_type == 1){
                $result = $this->validate($post_data,'AdminMenu');
            }else{
                $result = $this->validate($post_data,'AdminMenu.nourl');
            }
            if(true !== $result){
                //验证失败 输出错误信息
                $this->error($result);
            }

            //表单提交操作
            if(false !== $admin_menu_model->add($post_data)){
                $admin_menu_model->cache();
                $this->success('操作成功','index');
            }else{
                $this->error('添加失败请更新缓存后重试');
            }
        }

        $this->assign([
            'submenu' => $this->submenu,
            'pid' => $pid,
            'menu_name' => $menu_name,
            'menu_type' => $menu_type,
        ]);
        return $this->fetch();
    }

    /**
     * 修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少参数ID','index');
        }
        $data = AdminMenuModel::get($id);

        //判断菜单是否存在
        if(!$data){
            $this->error('菜单不存在，请更新后台菜单缓存后重试','index');
            $data->cache();
        }

        //获取菜单类型
        $top = $data->getTopId();
        $menu_name = $menu_type = '';
        $select = '<select name="data[pid]" lay-verify="required">';
        if ($data->pid) {
            if (in_array($data->pid, $top)) {
                $menu_type = 0;
                $menu_name = '分组菜单';
                $select = $data->parentSelect(1, $data->pid);
            } else {
                $menu_type = 1;
                $menu_name ='链接菜单';
                $select = $data->parentSelect(2, $data->pid);
            }
        } else {
            $menu_type = 0;
            $menu_name = '顶级菜单';
            $select = $data->parentSelect(0, $data->pid);
        }

        //判断表单提交
        if($this->request->isPost()){
            $post_data = $this->request->param('data/a');
            //验证菜单
            if($menu_type == 1){
                $result = $this->validate($post_data,'AdminMenu');
            }else{
                $result = $this->validate($post_data,'AdminMenu.nourl');
            }
            if(true !== $result){
                //验证失败 输出错误信息
                $this->error($result);
            }
            
            //表单提交操作
            if(false !== $data->allowField(true)->edit($post_data, $data->id)){
                $data->cache();
                $this->success('修改成功','index');
            }else{
                $this->error('添加失败请更新缓存后重试');
            }
        }

        // 分割uri
        $uri = [];
        if(trim($data->uri, '/')){
            $uri = explode("/", $data->uri);
        }

        $this->assign([
            'submenu' => $this->submenu,
            'uri' => $uri,
            'data' => $data,
            'select' => $select,
            'menu_name' => $menu_name,
            'menu_type' => $menu_type,
        ]);
        return $this->fetch();
    }

    /**
     * 删除
     * @access public
     * @return void
     */
    public function del($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少参数ID','index');
        }
        $admin_menu_model = new AdminMenuModel;
        $admin_menu_model->del($id);
        $admin_menu_model->cache();
        $this->success('删除成功','index');
    }

	/**
     * 更新缓存
     * @access public
     * @return void
     */
    public function cache(){
        $admin_menu_model = new AdminMenuModel;
        $admin_menu_model->cache();
        $this->success('菜单缓存更新成功','index');
    }
}